package com.example.earthquake;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

public class MainActivity extends Activity {
	
	private static final int MENU_PREFERENCES = Menu.FIRST + 1;
	private static final int MENU_UPDATE = Menu.FIRST + 2;
	
	private static final int SHOW_PREFERENCES = 1;
	
	public int minimumMagnitude = 0;
	public int updateFreq = 0;
	public boolean autoUpdated = false;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        updateFromPreferences();
        
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
        
        SearchView searchView = (SearchView) this.findViewById(R.id.searchView);
        searchView.setSearchableInfo(searchableInfo);
    }
    
    private void updateFromPreferences() {
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    	
    	autoUpdated = prefs.getBoolean(PreferencesActivity.PREF_AUTO_UPDATE, true);
    	
    	minimumMagnitude = Integer.parseInt(prefs.getString(PreferencesActivity.PREF_MIN_MAG, "3"));
    	updateFreq = Integer.parseInt(prefs.getString(PreferencesActivity.PREF_UPDATE_FREQ, "60"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	
    	if(requestCode == SHOW_PREFERENCES) {
			updateFromPreferences();
			
			FragmentManager fm = getFragmentManager();
			final EarthquakeListFragment earthquakeListFragment = (EarthquakeListFragment) fm.findFragmentById(R.id.earthquakeListFragment);
			earthquakeListFragment.refreshEarthquakes();
    	
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	
    	menu.add(0, MENU_PREFERENCES, Menu.NONE, R.string.menu_preferences);
    	
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	super.onOptionsItemSelected(item);
    	
    	switch (item.getItemId()) {
		case MENU_PREFERENCES:
			Class<? extends PreferenceActivity> c = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? PreferencesActivity.class : FragmentPreferences.class;
			Intent intent = new Intent(this, c);
			startActivityForResult(intent, SHOW_PREFERENCES);
			return true;

		}
    	return false;
    }
    
}
